package cur_userinfo

import (
	"github.com/gin-gonic/gin"
	jsoniter "github.com/json-iterator/go"
	"wwt-gin-api/app/global/response"
	"wwt-gin-api/app/global/variable"
	"wwt-gin-api/app/http/middleware/my_jwt"
)

// GetCurrentUserId 获取当前用户的id
// @context 请求上下文
func GetCurrentUserId(context *gin.Context) (int64, bool) {
	tokenKey := variable.ConfigYml.GetString("Token.BindContextKeyName")
	currentUser, exist := context.MustGet(tokenKey).(my_jwt.CustomClaims)
	return currentUser.UserId, exist
}

// GetUserInfoByToken 获取当前用户请求信息 兼容旺旺淘
func GetUserInfoByToken(context *gin.Context) *response.UserInfo {
	key := variable.ConfigYml.GetString("Token.BindContextKeyName")
	userInfo := &response.UserInfo{}
	userStr := context.GetString(key)
	_ = jsoniter.UnmarshalFromString(userStr, &userInfo)
	return userInfo
}
